package com.example.netty.nio.zeroCpy;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

public class NewIOClient {
    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1",7001);
        socketChannel.connect(inetSocketAddress);

        //获取文件channel
        FileChannel fileInputChannel = new FileInputStream("./dog.jpg").getChannel();

        long startTime = System.currentTimeMillis();
        //调用零拷贝api:windows调用一次最多只能穿8M，需要分批传
        long transferCount = fileInputChannel.transferTo(0, fileInputChannel.size(), socketChannel);
        //结果：总字结数：392715,耗时：16 3 2 2 2 3
        System.out.println("总字结数："+transferCount+",耗时："+(System.currentTimeMillis()-startTime));

        socketChannel.close();
        fileInputChannel.close();

    }
}

